/*
 * @Author: zengwj
 * @Date: 2022-08-08 16:47:18
 * @LastEditors: zengwj
 * @LastEditTime: 2022-08-17 15:09:31
 * @Description:
 */

import mount from './compiler/index.js'
import patch from './compiler/patch.js'
import renderHelper from './compiler/renderHelper.js'
import initComputed from './initComputed.js'
import initData from './initData.js'

export default function Vue(Options) {
	this._init(Options)
}

Vue.prototype._init = function (options) {
	console.log(options)
	this.$options = options
	initData(this)
	initComputed(this)
	renderHelper(this)
	// 在实例上安装 patch 函数
	this.__patch__ = patch
	// 如果配置el配置，调用mount方法编译模板
	if (this.$options.el) {
		this.$mount()
	}
}

Vue.prototype.$mount = function () {
	mount(this)
}
Vue.prototype._render = function () {
	// 给 render 函数绑定 this 上下文为 Vue 实例
	return this.$options.render.apply(this)
}
Vue.prototype._update = function (vnode) {
	const prevVnode = this._vnode
	this._vnode = vnode
	if (!prevVnode) {
		//没有老的vnode 说明是首次渲染
		this.$el = this.__patch__(this.$el, vnode)
	} else {
		this.$el = this.__patch__(prevVnode, vnode)
	}
}
